package com.android.plc.util;

import android.database.Cursor;

import com.android.plc.classholder.BasicItem.Type;
import com.android.plc.classholder.Profile;
import com.android.plc.classholder.Room;
import com.android.plc.classholder.Unit;
import com.android.plc.provider.PLC.Devices;
import com.android.plc.provider.PLC.Profiles;
import com.android.plc.provider.PLC.Rooms;

public class CursorUtil
{
	public static Room buildRoomFromCursor(Cursor cursor, int index)
	{
		Room room = null;
		if (cursor.moveToPosition(index))
		{
			int idIdx = cursor.getColumnIndex(Rooms._ID);
			int nameIdx = cursor.getColumnIndex(Rooms.NAME);
			int codeIdx = cursor.getColumnIndex(Rooms.CODE);
			int id = cursor.getInt(idIdx);
			String name = cursor.getString(nameIdx);
			int code = cursor.getInt(codeIdx);
			room = new Room(name, code);
			room._id = id;
		} else
		{
			room = new Room(Type.NORMAL_ITEM);
		}
		return room;
	}

	public static Profile buildProfileFromCursor(Cursor cursor, int index)
	{
		Profile profile = null;
		if (cursor.moveToPosition(index))
		{
			int idIdx = cursor.getColumnIndex(Profiles._ID);
			int nameIdx = cursor.getColumnIndex(Profiles.NAME);
			int cmdIdx = cursor.getColumnIndex(Profiles.COMMAND);
			int id = cursor.getInt(idIdx);
			String name = cursor.getString(nameIdx);
			int cmd = cursor.getInt(cmdIdx);
			profile = new Profile(name, cmd);
			profile._id = id;
		} else
		{
			profile = new Profile(Type.NORMAL_ITEM);
		}
		return profile;
	}

	public static Unit buildUnitFromCursor(Cursor cursor, int index)
	{
		Unit unit = null;
		if (cursor != null && cursor.moveToPosition(index))
		{
			int idIdx = cursor.getColumnIndex(Devices._ID);
			int nameIdx = cursor.getColumnIndex(Devices.NAME);
			int cmdIdx = cursor.getColumnIndex(Devices.CODE);
			int roomIdx = cursor.getColumnIndex(Devices.ROOM_ID);
			int typeIdx = cursor.getColumnIndex(Devices.TYPE);
			int subtypeIdx = cursor.getColumnIndex(Devices.SUB_TYPE);
			int id = cursor.getInt(idIdx);
			String name = cursor.getString(nameIdx);
			int cmd = cursor.getInt(cmdIdx);
			int room = cursor.getInt(roomIdx);
			int type = cursor.getInt(typeIdx);
			int subtype = cursor.getInt(subtypeIdx);
			unit = new Unit(name, type, subtype, cmd, room);
			unit._id = id;
		} else
		{
			unit = new Unit(Type.NORMAL_ITEM);
		}
		return unit;
	}
}
